package com.youlemall.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youlemall.common.pojo.PageResult;
import com.youlemall.mapper.SpecificationMapper;
import com.youlemall.mapper.SpecificationOptionMapper;
import com.youlemall.pojo.Specification;
import com.youlemall.pojo.SpecificationOption;
import com.youlemall.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**规格名称实现接口
 * 2 * @Author: kevin
 * 3 * @Date: 2019/7/14 22:29
 * 4
 */

@Service
@Transactional
public class SpecificationServiceImpl implements SpecificationService {

	@Autowired
	private SpecificationMapper specificationMapper;

	@Autowired
	private SpecificationOptionMapper specificationOptionMapper;

	@Override
	public void save(Specification specification) {
		try {
			specificationMapper.insertSelective(specification);
			specificationOptionMapper.save(specification);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(Specification specification) {
		try {
			specificationMapper.updateByPrimaryKeySelective(specification);
			SpecificationOption spo = new SpecificationOption();
			spo.setSpecId(specification.getId());
			int delete = specificationOptionMapper.delete(spo);
			System.out.println(delete);
			specificationOptionMapper.save(specification);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void delete(Serializable id) {

	}

	@Override
	public void deleteAll(Serializable[] ids) {
		try {
			Example example = new Example(SpecificationOption.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andIn("specId", Arrays.asList(ids));
			specificationOptionMapper.deleteByExample(example);
			example=new Example(Specification.class);
			criteria=example.createCriteria();
			criteria.andIn("id",Arrays.asList(ids));
			specificationMapper.deleteByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public Specification findOne(Serializable id) {
		return null;
	}

	@Override
	public List<Specification> findAll() {
		return null;
	}

	@Override
	public PageResult findByPage(Specification specification, int page, int rows) {
		PageInfo<Specification> pageInfo = PageHelper.startPage(page, rows).doSelectPageInfo(new ISelect() {
			@Override
			public void doSelect() {
				specificationMapper.findByspecName(specification);
			}
		});
		return new PageResult(pageInfo.getPages(),pageInfo.getList());
	}

	@Override
	public List<Map<String, Object>> findSpeNameAndId() {
		try {
			return specificationMapper.findSpeNameAndId();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
